<?php

/**
 * 绑定银行卡
 * 
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/19
 * Time: 下午10:49
 */
class Bank_card extends MY_Api_Controller
{
    /**
     * 已绑定银行卡列表
     */
    public function index()
    {
        $uid = $this->uid;


        $this->load->service('finance/service_finance_bank_card');
        $data = $this->service_finance_bank_card->index($uid);



        $r_data = array();
        foreach ($data['data'] as $k => $v)
        {
            $tmp = array_unique(explode('-', $v['bank']));
            $r_data[] = array
            (
                'bid'      => $v['bid'],
                'name'     => '(尾号'.(substr($v['card_no'], -4, 4)).')'.(isset($tmp[0]) ? $tmp[0] : ''),
                'summary'  => '提现到'.$v['bank'].', 手续费率'.(isset($v['rate']) ? $v['rate'] : '0.1').'%',
                'rate'     => $v['rate'],
                'blankname'=>$v['bank'],
                'endnumber'=>  substr($v['card_no'], -4, 4),
            );
        }


        $this->json_output($r_data, $data['status']);
    }


    /**
     * 银行列表
     */
    public function bank()
    {
        $this->load->service('finance/service_finance_bank_card');
        $data = $this->service_finance_bank_card->bank();


        foreach ($data['data'] as $k => $v)
        {
            unset($data['data'][$k]['summary']);
            unset($data['data'][$k]['rate']);
            unset($data['data'][$k]['create_time']);
        }


        $this->json_output($data['data'], $data['status']);
    }


    public function bind()
    {
        $uid          = $this->uid;
        $cardholder   = trim($this->input->get_post('cardholder'));
        $card_no      = trim($this->input->get_post('card_no'));
        $bank_id      = trim($this->input->get_post('bank_id'));
        $bank_branch  = trim($this->input->get_post('bank_branch'));
        $account_area = trim($this->input->get_post('account_area'));
        $mobile       = trim($this->input->get_post('mobile'));
        $code         = trim($this->input->get_post('code'));


        $param = array
        (
            'uid'          => $uid,
            'cardholder'   => $cardholder,
            'card_no'      => $card_no,
            'bank_id'      => $bank_id,
            'bank_branch'  => $bank_branch,
            'account_area' => $account_area,
            'mobile'       => $mobile,
            'code'         => $code,
        );


        $this->load->service('finance/service_finance_bank_card');
        $data = $this->service_finance_bank_card->bind($param);


        unset($data['data']['ip']);


        if (isset($data['data']['currency']))
        {
            $currency = config_item('currency');
            $data['data']['currency'] = isset($currency[$data['data']['currency']]['name']) ?
                $currency[$data['data']['currency']]['name'] : '未知币种';
        }


        $this->json_output($data['data'], $data['status']);
    }


    public function unbind()
    {
    	$uid = $this->uid;
    	$bid = trim($this->input->get_post('bid'));
    
    	$param = array
    	(
            'uid' => $uid,
            'bid' => $bid,
    	);
    
    
    	$this->load->service('finance/service_finance_bank_card');
    	$data = $this->service_finance_bank_card->unbind($param);


    	$this->json_output($data['data'], $data['status']);
    }


    public function check_bank()
    {
        $card_no = trim($this->input->get_post('card_no'));


        $this->load->service('finance/service_finance_bank_card');
        $data = $this->service_finance_bank_card->check_bank($card_no);


        $this->json_output($data['data'], $data['status']);
    }
}